#include <QtGui/QApplication>
#include <QyDebug.h>
#include <QyAbstractRender.h>
#include <QyGLInputController.h>
#include <QyGLViewWidget.h>
#include "QyRender.h"
#include "QyRenderDecorationItem.h"
#include "QyRenderLayer.h"
#include "QyRenderItem.h"
#include "QyTexturePool.h"

/**************************************************************************************************/

class TestRender : public QyGL::AbstractRender {
public:
    TestRender() {

        charasterLayer.setZ(0);
        environmentLayer.setZ(1);
        wallLayer.setZ(2);

        painter.addLayer(&charasterLayer);
        painter.addLayer(&environmentLayer);
        painter.addLayer(&wallLayer);

    }
    ~TestRender() {

        painter.removeLayer(&charasterLayer);
        painter.removeLayer(&environmentLayer);
        painter.removeLayer(&wallLayer);

    }

    void initialize( QyGL::ViewWidget * viewWidget ) {

        glClearColor( 1, 1, 1, 1 );
        glPointSize(5);
        glPolygonOffset( 1.0, 2.0 );
        glLineWidth(1);
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
        glEnable(GL_ALPHA_TEST);
        glEnable(GL_BLEND);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_POLYGON_OFFSET_FILL);
        glEnable(GL_TEXTURE_2D);

        texturePool.basePath = qApp->applicationDirPath() + "/../data/textures";
        texturePool.glWidget = viewWidget;
        texturePool.loadFrom("./");

        QyRender::DecorationItem * item = new QyRender::DecorationItem;
        QyRender::ItemGeometry geometry;

        geometry.append( QPointF( -4, -4 ) );
        geometry.append( QPointF( -4,  4 ) );
        geometry.append( QPointF(  4,  4 ) );
        geometry.append( QPointF(  4, -4 ) );

        item->setGeometry(geometry);
        item->setTexture( texturePool.textures["easy-pen-box.png"] );
//         item->setTexture( texturePool.textures["pw-logo.png"] );
        item->setColor(Qt::blue);
        item->setLayer(&charasterLayer);

        new QyGL::BasicCamViewController(viewWidget);

    }
//     void resize( int, int ) {}
    void paint( QyGL::ViewWidget * ) {

        painter.paint();

    }

    QyRender::Layer charasterLayer; // players, enemies
    QyRender::Layer environmentLayer; // nature, decorations
    QyRender::Layer wallLayer; // building's
    QyRender::Painter painter; // painter
    QyTexturePool texturePool;

};

/**************************************************************************************************/

class TestView : public QyGL::ViewWidget {
public:
    explicit TestView() {

        setRender(&render);

    }

    TestRender render;

};

/**************************************************************************************************/

int main( int argCount, char ** args ) {

    QApplication app( argCount, args );
    TestView view;

    view.show();

    QpwAuth::LoginDatabase loginDatabase( qApp->applicationDirPath() + "/../data/userdb.xlm");

    loginDatabase.add( QpwAuth::Login( "dodge", "123", QpwAuth::Login::DeveloperAccessLevel ) );
    loginDatabase.add( QpwAuth::Login("guest") );
    loginDatabase.add( QpwAuth::Login( "admin", "456", QpwAuth::Login::AdminAccessLevel ) );
    loginDatabase.add( QpwAuth::Login( "user", "987", QpwAuth::Login::UserAccessLevel ) );
    loginDatabase.save();


    return app.exec();
}
